package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.DistanceUtils;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.Point;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    //寻主消息对象
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    //百度AI审核对象
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    //寻主消息日志对象
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    //店铺对象
    @Autowired
    private ShopMapper shopMapper;
    //管理员对象
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {

    //保存寻主消息
        //先调用工具类获取公钥解密，从request里得到登陆人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //再通过登录信息找到这个用户
        User user = userMapper.findByLogininfoId(logininfo.getId());
        //将用户的id设置到寻主消息的用户id
        searchMasterMsg.setUserId(user.getId());
        //直接保存寻主消息
        searchMasterMsgMapper.add(searchMasterMsg);

    //通过百度AI智能审核文字和图片
        Boolean boo1 = baiduAiAuditService.textAudit(searchMasterMsg.getTitle() +
                searchMasterMsg.getName() +
                searchMasterMsg.getCoatColor());
        Boolean boo2 = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());

    //审核成功
        //创建日志对象
        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        //判断
        if(boo1 && boo2){
            //如果两个都审核通过,就修改寻主消息状态为1
            searchMasterMsg.setState(1);
            //修改寻主消息日志为1 保存
            log.setState(1);
            log.setNote("审核通过");
            searchMasterMsgAuditLogMapper.add(log);

        //然后分配最近的店铺  分配失败：1.放入寻主池改状态1-3
            List<Shop> shops = shopMapper.findAll();
            //根据寻主消息中的address找到这个店铺 在调用工具类比对找到最近的店铺
            Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
            Shop nearestShop = DistanceUtils.getNearestShop(point, shops);
            //判断是否为空
            if(nearestShop==null){//表示没有找到最近店铺
                searchMasterMsg.setState(3);
            }else {//找到了 把最近的店铺id设置到寻主消息中的店铺Id
                searchMasterMsg.setShopId(nearestShop.getId());
                //然后根据最近店铺的管理员Id找到店铺管理员
                Employee admin = employeeMapper.findById(nearestShop.getAdminId());
                //然后发送短信/邮件给他
                //SmsUtils.sendSms(admin.getPhone(),"你有新的订单请去"+searchMasterMsg.getAddress()+"接收宠物");
                System.out.println("你有新的订单请去"+searchMasterMsg.getAddress()+"接收宠物");
            }
        //更新日志
        searchMasterMsgMapper.update(searchMasterMsg);
        }else {
            //审核失败
           /* searchMasterMsg.setState(-1);
            searchMasterMsgMapper.update(searchMasterMsg);*/
            //修改寻主消息日志状态为0 保存
            log.setState(0);
            log.setNote("审核失败");
            searchMasterMsgAuditLogMapper.add(log);

            //审核失败就删除寻主消息以免重复向数据库添加数据
            searchMasterMsgMapper.delete(searchMasterMsg.getId());
            System.out.println("你的寻主消息发布失败，请修改后再发布");
            //然后发送短信/邮件给他
            //SmsUtils.sendSms(user.getPhone(),"你的信息发布失败，请修改后再发布");
            return AjaxResult.me().setSuccess(false).setMessage("寻主消息发布失败，请检查是否有违规内容");
        }

        return new AjaxResult();
    }
}
